package com.damuzee.system.system.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.mybatis.sql.page.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.damuzee.system.system.domain.PermOrg;
import com.damuzee.system.system.service.PermOrgService;

@Controller
public class OrgAct {

	@RequestMapping(value="/org/v_main.do")
	public String main(){
		return "/org/main";
	}

	@RequestMapping(value="/org/v_center.do")
	public String center(){
		return "/org/center";
	}
	
	@RequestMapping(value="/org/v_left.do")
	public String left(){
		return "/org/left";
	}
	
	@RequestMapping(value="/org/v_tree.do")
	public String tree(String root, HttpServletRequest request,
			HttpServletResponse response, ModelMap model){
		
		boolean isRoot;
		// jquery treeview的根请求为root=source
		if (StringUtils.isBlank(root) || "source".equals(root)) {
			isRoot = true;
		} else {
			isRoot = false;
		}
		model.addAttribute("isRoot", isRoot);
		List<PermOrg> list = null;
		if (isRoot) {
			list = orgService.getChildList(new Long(1));
		} else {
			Integer rootId = Integer.valueOf(root);
			list = orgService.getChildList(new Long(rootId));
		}
		model.addAttribute("list", list);
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/json;charset=UTF-8");
		return "/org/tree";
	}
	
	
	@RequestMapping(value="/org/v_list.do")
	public String list(Integer pageNo,String root,HttpServletRequest request, ModelMap model){
		
		PermOrg org = new PermOrg();
		if (StringUtils.isBlank(root) || "source".equals(root)) {
			org.setOrgId((long) 1);
		} else {
			Integer rootId = Integer.valueOf(root);
			org.setOrgId((long) rootId);
		}
		
		Page<PermOrg> page = new Page<PermOrg>();
		page.setCurrentPage(pageNo);
		
		page = orgService.getPermOrgList(page, org);
		
		model.addAttribute("page", page.getPage());
		return "/org/list";
	}
	
	@Resource
	private PermOrgService orgService ;
	
}
